unit Saper;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

const
  N=30;
  M=300;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  arr: array [0..29, 0..29] of TButton;


implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
i, j, ir, jr, t: integer;
begin
  for j:=0 to N-1 do
  begin
    for i:= 0 to N-1 do
    begin
      arr[j, i] := TButton.Create(Form1);
      arr[j, i].Width := 20;
      arr[j, i].Height := 20;
      arr[j, i].Parent := Form1;
      arr[j, i].Top := 20*i;
      arr[j, i].Left := 20*j;
      arr[j, i].OnClick:=Button2Click;
    end;
  end;
  t:=0;
  while (t<M) do
  begin
    randomize;
    ir:=random(N);
    jr:=random(N);
    if (arr[ir, jr].Caption<>' ') then
    begin
      arr[ir, jr].Caption:=' ';
      t:=t+1;
    end;
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
i, j, col1, col2, col3, col4, col5, col6, col7, col8: integer;
begin
 for i:=0 to N-1 do
  begin
    for j := 0 to N-1 do
    begin
        if (Sender = arr[i, j]) then
        begin
          if (arr[i, j].Caption=' ') then
          begin
            arr[i, j].Caption:='M';
            ShowMessage('BOOOOOM!');
          end
          else
          begin
            col1:=0;
            col2:=0;
            col3:=0;
            col4:=0;
            col5:=0;
            col6:=0;
            col7:=0;
            col8:=0;
            if (i<N) then
            begin
              if (arr[i+1, j].Caption=' ') or (arr[i+1, j].Caption='M') then
                col1:=1;
            end;
            if (i>0) then
            begin
              if (arr[i-1, j].Caption=' ') or (arr[i-1, j].Caption='M') then
                col2:=1;
            end;
            if (j>0) then
            begin
              if (arr[i, j-1].Caption=' ') or (arr[i, j-1].Caption='M')  then
                col3:=1;
            end;
            if (j<N) then
            begin
              if (arr[i, j+1].Caption=' ') or (arr[i, j+1].Caption='M') then
                col4:=1;
            end;
            if (i>0) and (j>0) then
            begin
              if (arr[i-1, j-1].Caption=' ') or (arr[i-1, j-1].Caption='M') then
                col5:=1;
            end;
            if (i<N) and (j<N) then
            begin
              if (arr[i+1, j+1].Caption=' ') or (arr[i+1, j+1].Caption='M') then
                col6:=1;
            end;
            if (i<N) and (j>0) then
            begin
              if (arr[i+1, j-1].Caption=' ') or (arr[i+1, j-1].Caption='M') then
                col7:=1;
            end;
            if (i>0) and (j<N) then
            begin
              if (arr[i-1, j+1].Caption=' ') or (arr[i-1, j+1].Caption='M') then
                col8:=1;
            end;
            arr[i, j].Caption:=IntToStr(col1+col2+col3+col4+col5+col6+col7+col8);
          end;
        end;
    end;
  end;
end;

end.
